package com.flexcloudsdk.web;
import java.io.IOException;

import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.channel.ChannelFailureException;
import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.appengine.api.channel.ChannelServicePb;

public class CloudChannelServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(CloudChannelServlet.class.getName());
	@SuppressWarnings("unused")
	private ChannelServiceFactory factory;
	@SuppressWarnings("unused")
	private ChannelServicePb pb;
    @SuppressWarnings("unused")
	private ChannelMessage msg;	
    @SuppressWarnings("unused")
	private ChannelFailureException failure;
	private String sourceMethod;
	private String sourceClass;
	
	public CloudChannelServlet() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudChannelServlet.class.getName();
		log.info("*****CloudChannelServlet constructor********");
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		resp.setContentType("text/xml");
		PrintWriter writer = resp.getWriter();
		writer.println("<?xml version=\"1.0\"?>");
		writer.println("<root>");

		String operation = req.getParameter("operation");
		if (operation == null) {
			handleNullOperation(writer, req);
		} else if (operation.isEmpty()) {
			handleEmptyOperation(writer, req);
		}
		writer.println("</root>");
		log.exiting(sourceClass, sourceMethod);
	}


	private void handleNullOperation(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		log.warning("operation is null");
		
		writer.println("<result>No operation found while calling "
				+ req.getServerName() + "servlet</result>");
		
		log.exiting(sourceClass, sourceMethod);
	}
	
	private void handleEmptyOperation(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		log.warning("operation is empty");
	
		writer.println("<result>Must pass an operation in calling "
				+ req.getServerName() + "servlet</result>");
	}
}
